package com.snow.controller;

import com.github.pagehelper.PageInfo;
import com.snow.constant.Types;
import com.snow.constant.Webconst;
import com.snow.pojo.dto.ArchiveDto;
import com.snow.pojo.dto.MetaDto;
import com.snow.pojo.dto.cond.ContentCond;
import com.snow.pojo.model.ContentDomain;
import com.snow.pojo.model.UserDomain;
import com.snow.service.content.ContentService;
import com.snow.service.meta.MetaService;
import com.snow.service.site.SiteService;
import com.snow.utils.MapCache;
import com.snow.utils.TaleUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-11
 **/
public abstract class BaseController {
    @Autowired
    private SiteService siteService;
    @Autowired
    private MetaService metaService;
    @Autowired
    private ContentService contentService;

    protected MapCache cacheMap = MapCache.single();

    private static Logger logger = LoggerFactory.getLogger(BaseController.class);


    public BaseController title(Model model,String title){
        logger.info("Enter in BaseController title method");
        model.addAttribute("title",title);
        return this;
    }

    /**
     * 页面需要的公共部分内容
     * @param model
     * @param contentCond
     * @return
     */
    public BaseController blogPageBaseData(Model model, ContentCond contentCond){
        logger.debug("BaseController =====>blogBaseData Method");

        List<MetaDto> tags = metaService.getMetasByType(Types.TAG.getType(), null, Webconst.MAX_POSTS);
        List<MetaDto> categories = metaService.getMetasByType(Types.CATEGORY.getType(), null, Webconst.MAX_POSTS);
        List<MetaDto> links = metaService.getMetasByType(Types.LINK.getType(), null, Webconst.MAX_POSTS);
        model.addAttribute("links",links);
        model.addAttribute("tags",tags);
        model.addAttribute("categories",categories);

        PageInfo<ContentDomain> recentlyArticle = contentService.getRecentlyArticle(1, 10);
        model.addAttribute("recentlyArticles",recentlyArticle);

        List<ArchiveDto> archive = siteService.getArchive(contentCond);
        model.addAttribute("archives",archive);

        return this;
    }

    /**
     * 获取请求绑定的登录对象
     * @param request
     * @return
     */
    public UserDomain user (HttpServletRequest request){
        return TaleUtils.getLoginUser();
    }
    public Integer getUid(HttpServletRequest request){
        return this.user(request).getUid();
    }
    public String join(String [] arr){
        String arr2[] = arr;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < arr2.length ; i++) {
            builder.append(",").append(arr2[i]);
        }
        return builder.length() > 0 ? builder.substring(1):builder.toString();
    }
    protected BaseController userInfo(Model model){
        UserDomain loginUser = this.cacheMap.get("loginUser");
        model.addAttribute("loginUser",loginUser);
        return this;
    }


}
